package com.fatcat.easybuy.service.impl;

import com.fatcat.easybuy.exception.BusinessException;
import com.fatcat.easybuy.mapper.EasyBuyUserMapper;
import com.fatcat.easybuy.pojo.EasyBuyUser;
import com.fatcat.easybuy.service.EasyBuyUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author Fatcat
 * @description 针对表【easybuy_user】的数据库操作Service实现
 * @createDate 2025-09-10 11:10:26
 */
@Service
public class EasyBuyUserServiceImpl implements EasyBuyUserService {

    @Autowired
    private EasyBuyUserMapper easyBuyUserMapper;

    @Override
    public EasyBuyUser getUserByLoginName(String loginName) {
        return easyBuyUserMapper.selectUserByLoginName(loginName);
    }

    @Override
    public EasyBuyUser getUserByEmail(String email) {
        return easyBuyUserMapper.selectUserByEmail(email);
    }

    @Transactional
    @Override
    public boolean addUser(EasyBuyUser easyBuyUser) {
        // 校验登录名是否已存在
        if (getUserByLoginName(easyBuyUser.getLoginName()) != null) {
            throw new BusinessException("登录名已被占用");
        }
        // 校验邮箱是否已存在
        if (getUserByEmail(easyBuyUser.getEmail()) != null) {
            throw new BusinessException("邮箱已被注册");
        }
        try {
            return easyBuyUserMapper.insertUser(easyBuyUser) > 0;
        } catch (Exception e) {
            throw new BusinessException("新增用户失败,用户已存在", e);
        }
    }

    @Override
    public EasyBuyUser getUserById(Long id) {
        return easyBuyUserMapper.selectUserById(id);
    }

    @Transactional
    @Override
    public boolean updateUser(EasyBuyUser easyBuyUser) {
        if (easyBuyUser.getId() == null) {
            throw new BusinessException("id不能为空");
        }
        try {
            return easyBuyUserMapper.updateUser(easyBuyUser) > 0;
        } catch (Exception e) {
            throw new BusinessException("更新用户失败,用户不存在", e);
        }
    }

    @Transactional
    @Override
    public boolean deleteUser(Long id) {
        try {
            return easyBuyUserMapper.deleteUser(id) > 0;
        } catch (Exception e) {
            throw new BusinessException("删除用户失败,用户不存在", e);
        }
    }

    @Override
    public PageInfo<EasyBuyUser> getAllUser(Map<String, Object> params) {
        PageHelper.startPage(params);
        List<EasyBuyUser> list = easyBuyUserMapper.selectAllUser(params);
        return new PageInfo<>(list);
    }

    @Transactional
    @Override
    public boolean updatePassword(Map<String, Object> params) {
        try {
            return easyBuyUserMapper.updatePassword(params) > 0;
        } catch (Exception e) {
            throw new BusinessException("更新密码失败,用户不存在", e);
        }
    }

    @Transactional
    @Override
    public boolean updateEmail(Map<String, Object> params) {
        try {
            return easyBuyUserMapper.updateEmail(params) > 0;
        } catch (Exception e) {
            throw new BusinessException("更新邮箱失败,用户不存在", e);
        }
    }
}




